package com.test.latitudex;

import java.util.Locale;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.test.latitudex.utilities.LocationBroadcastReceiver;
import com.test.latitudex.utilities.RegistrationManager;

public class MainActivity extends FragmentActivity {
	
	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	static ViewPager mViewPager;
	static Context ctx;
	static ProgressDialog dialog;
	@SuppressWarnings("unused")
	private static final String LOG_TAG = "MainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		ctx = this;
		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		if(this.getIntent().getBooleanExtra(RegistrationManager.IS_LOGGED_IN, false)){
			refreshData();
		}
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setCurrentItem(1);
		
		
		
	}
	
	
	public static void pager(int index){
		mViewPager.setCurrentItem(index);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
		if(item.getTitle().equals(getResources().getString(R.string.action_logout))){
			Intent intent = new Intent(this,LoginActivity.class);
			intent.putExtra(RegistrationManager.FROM_LOGOUT, true);
			startActivity(intent);
			finish();
		}else if(item.getTitle().equals(getResources().getString(R.string.action_settings))){
			
		}else if(item.getTitle().equals(getResources().getString(R.string.action_start))){
			LocationBroadcastReceiver.SetAlarm(this);
		}else if(item.getTitle().equals(getResources().getString(R.string.action_stop))){
			LocationBroadcastReceiver.CancelAlarm(this);
		}else if(item.getTitle().equals(getResources().getString(R.string.action_refresh))){
			refreshData();
		}
		
		return super.onOptionsItemSelected(item);
    }
	
	public void refreshData(){
		synchronized (this) {
			runOnUiThread(new Runnable(){

				@Override
				public void run() {
					dialog = ProgressDialog.show(ctx, "", "Downloading data... Please wait!", true);
				}
				
			});
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						InvitationsFragment.refreshInviteList();
						FriendsFragment.refreshFriendData();
						dialog.dismiss();
					} catch (Exception e) {
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								Toast.makeText(ctx,
										"Network communication unsuccesful. Please check network connection!",
										Toast.LENGTH_LONG).show();
							}

						});
					}
					
				}

			}).start();

		}
	}
	
	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
			super(fragmentManager);
		}

		@Override
		public android.support.v4.app.Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a DummySectionFragment (defined as a static inner class
			// below) with the page number as its lone argument.
			switch(position){
			case 0:{
				InvitationsFragment fragment = new InvitationsFragment();
				return fragment;
			}
			case 1:{
				FriendsFragment fragment = new FriendsFragment();
				return fragment;
			}
			case 2:{
				MyMapFragment fragment = new MyMapFragment();
				return fragment;
			}
			default:{
				return null;
			}
			}
			
		}

		@Override
		public int getCount() {
			// Show 3 total pages.
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase(l);
			case 1:
				return getString(R.string.title_section2).toUpperCase(l);
			case 2:
				return getString(R.string.title_section3).toUpperCase(l);
			}
			return null;
		}
	}

	

}